package com.atguigu.chapter11.function;

import com.atguigu.bean.WaterSensor;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.functions.TableAggregateFunction;
import org.apache.flink.util.Collector;

import static org.apache.flink.table.api.Expressions.$;
import static org.apache.flink.table.api.Expressions.call;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/7/26 9:11
 */
public class Flink03_Function_TableAgg {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        DataStreamSource<WaterSensor> stream = env.fromElements(new WaterSensor("sensor_1", 1000L, 10),
                                                                new WaterSensor("sensor_1", 2000L, 20),
                                                                new WaterSensor("sensor_2", 3000L, 30),
                                                                new WaterSensor("sensor_1", 4000L, 40),
                                                                new WaterSensor("sensor_1", 5000L, 50),
                                                                new WaterSensor("sensor_2", 6000L, 60));
        
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        
        Table table = tEnv.fromDataStream(stream);
        
        // 接下来我们写一个自定义TableAggregateFunction，用来提取每个sensor最高的两个温度值(两行)。
        table
            .groupBy($("id"))
//            .flatAggregate(call(Top2.class, $("vc")).as("value"))
            .flatAggregate(call(Top22.class, $("vc")).as("v", "rank"))
            .select($("id"), $("v"), $("rank"))
            .execute()
            .print();
    }
    
    public static class FirstSecond {
        public Integer first = 0;
        public Integer second = 0;
    }
    
    public static class Top22 extends TableAggregateFunction<Tuple2<Integer, Integer>, FirstSecond> {
        
        @Override
        public FirstSecond createAccumulator() {
            return new FirstSecond();
        }
        
        // 聚合函数
        public void accumulate(FirstSecond acc, Integer v) {
            if (v > acc.first) {
                acc.second = acc.first;
                acc.first = v;
            } else if (v > acc.second) {
                acc.second = v;
            }
        }
        
        // 指标
        public void emitValue(FirstSecond acc, Collector<Tuple2<Integer, Integer>> collector) {
            collector.collect(Tuple2.of(acc.first, 1));  // 调用几次就有几行
            
            if (acc.second > 0)
                collector.collect(Tuple2.of(acc.second, 2));  // 调用几次就有几行
        }
    }
    
    public static class Top2 extends TableAggregateFunction<String, FirstSecond> {
        
        @Override
        public FirstSecond createAccumulator() {
            return new FirstSecond();
        }
        
        // 聚合函数
        public void accumulate(FirstSecond acc, Integer v) {
            if (v > acc.first) {
                acc.second = acc.first;
                acc.first = v;
            } else if (v > acc.second) {
                acc.second = v;
            }
        }
        
        // 指标
        public void emitValue(FirstSecond acc, Collector<String> collector) {
            collector.collect("最大水位: " + acc.first);  // 调用几次就有几行
            collector.collect("第二大水位: " + acc.second);  // 调用几次就有几行
        }
    }
    
}
/*
hello  5
atguigu  6

 */
